package com.wu.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.wu.dao.QuestionDao;
import com.wu.pojo.po.paperPo.Question;
import com.wu.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description:
 * Author:Wuzhengru
 * Date:2021/02/25-0:55
 */
@Transactional
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;

    @Override
    public List<Question> queryQuestion() {
        return questionDao.queryQuestion();
    }

    @Override
    public List<Question> queryQuestionByPaperId(String paperId) {
        return questionDao.queryQuestionByPaperId(paperId);
    }

    @Override
    public Question queryQuestionById(String id) {
        return questionDao.queryQuestionById(id);
    }

    @Transactional
    @Override
    public boolean insertQuestion(Question question) {
        if (question != null && !"".equals(question.getId())) {
            try {
                int i = questionDao.insertQuestion(question);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("插入问题失败！" + question);
                }
            } catch (Exception e) {
                throw new RuntimeException("插入问题失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("插入问题失败，问题id不能为空！");
        }
    }

    @Transactional
    @Override
    public boolean insertQuestion(List<Question> questionList) {
        int j = 0;
        for (Question question : questionList) {
            if (question != null && !"".equals(question.getId())) {
                int i = questionDao.insertQuestion(question);
                j += i;
            }
        }
        if (j == questionList.size()) {//判断插入成功数
            return true;
        }
        throw new RuntimeException("批量插入问题失败");//=================为啥抛出的异常没有捕获
    }

    @Transactional
    @Override
    public boolean updateQuestion(Question question) {
        if (question != null && !"".equals(question.getId())) {
            try {
                int i = questionDao.updateQuestion(question);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:更新问题失败！" + question);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:更新问题失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:更新问题失败，问题id不能为空！");
        }
    }

    @Transactional
    @Override
    public boolean deleteQuestion(String id) {
        if (id != null && !"".equals(id)) {
            try {
                int i = questionDao.deleteQuestion(id);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:删除问题失败！" + id);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:删除问题失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:删除问题失败，问题id不能为空！");
        }
    }

    @Transactional
    @Override
    public boolean deleteQuestionsByPaperId(String id) {
        if (id != null && !"".equals(id)) {
            try {
                int i = questionDao.deleteQuestionsByPaperId(id);
                if (i >= 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:删除问题失败！" + id);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:删除问题失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:删除问题失败，试卷id不能为空！");
        }
    }


    @Override
    public List<Question> getQuestionsByPaperIdAndQuestionType(String paperId, Integer questionType) {

        if (paperId != null && !"".equals(paperId) && (questionType == 1 || questionType == 2 || questionType == 3)) {
            return questionDao.getQuestionsByPaperIdAndQuestionType(paperId, questionType);
        }
        throw new RuntimeException("根据PaperId和问题类型获取问题失败！" + "ID:" + paperId + "   questionType:" + questionType);
    }


    @Override
    public String queryQuestionByPaperIdAndQuestionNo(String paperId, String questionId) {
        return questionDao.queryQuestionByPaperIdAndQuestionNo(paperId,questionId);
    }
}
